# 0
# Load reqired packages, set working directory

library(ggplot2)
library(foreign)
library(gridExtra)

setwd("/Users/samharper/OneDrive - McGill University")

# 1
# plot for age

# read in results file
d <- read.dta("age-race.dta")

# define demographic groups
# race
d$racen <- as.numeric(d$race)
d$racen <- factor(d$racen, levels = c(1,2), labels = c("Non-Hispanic Black", "Non-Hispanic White"))

# age
d$age11n <- as.numeric(d$age)
d$age11n <- factor(d$age11n, levels = c(12,11,10,9,8,7,6,5,4,3,2,1), labels = c("Total","  85+", "75-84", "65-74", "55-64", "45-54", "35-44", "25-34", "15-24", "5-14", "  1-4", "  0-1"))
d$pctage <- round(d$pctgap*100, digits=0)

# gender
d$sexn <- as.numeric(d$sex)
d$sexn <- factor(d$sexn, levels = c(1,2), labels = c("Women", "Men"))

# make plot for age-specific contribution (Appendix Fig 1)
ggplot(d, aes(y=te, factor(age11n), label=pctage)) + 
  geom_bar(stat="identity", colour="black", fill="grey80", width=0.5) + 
  geom_text(y=.35, hjust=1, size=2.5) + 
  annotate("text", x = 13, y = .35, label = "% of Total", hjust=1, size=2.5) +
  geom_segment(aes(x = 0, y = 0.005, xend = 0, yend = 0.02), size=0.25, arrow = arrow(length = unit(0.1, "cm"))) +
  annotate("text", x = 0, y = .025, label = "Decreased life expectancy", hjust=0, size=2) +
  geom_segment(aes(x = 0, y = -0.005, xend = 0, yend = -0.02), size=0.25, arrow = arrow(length = unit(0.1, "cm"))) +
  annotate("text", x = 0, y = -.025, label = "Increased", hjust=1, size=2) +
  xlab("Age group") + ylab("Years of life expectancy") + 
  facet_wrap( ~ class, ncol=2) +
  coord_flip() + scale_x_discrete(expand = c(0.1, 0.1)) + 
  scale_y_continuous(breaks=c(-.1,0,.1, .2),limits=c(-.1,.35)) + 
  theme_bw() +
  theme(axis.text.y=element_text(size=8), axis.ticks=element_blank(), strip.background = element_blank(), panel.grid.major.x = element_line(colour="grey80"), panel.grid.major.y = element_line(colour="white"), panel.grid.minor = element_line(colour="white"),panel.border = element_rect(colour = "black"))

# save plot
ggsave("age-race.png", dpi=600, width=6.5, height=5)


# 2
# cause of death plot

# load results file
dc <- read.dta("cod-race-plots.dta")

# define demographic variables
# race
dc$racen <- as.numeric(dc$race)
dc$racen <- factor(dc$racen, levels = c(1,2), labels = c("Non-Hispanic Black", "Non-Hispanic White"))

# cause of death
dc$cod14n <- as.numeric(dc$cod)
dc$cod14n <- factor(dc$cod14n, levels = c(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1), labels = c("Total","All other causes", "Homicide","Suicide","Unintentional Poisoning","Motor vehicle crashes", "Kidney Disease", "Liver Disease", "Chronic Respiratory Disease", "HIV","Flu/Pneumonia", "Alzheimer's", "Diabetes", "Cancers", "Cardiovascular"))
dc$pctgapc <- round(dc$pctgapc*100, digits=0)

# gender
dc$sexn <- as.numeric(dc$sex)
dc$sexn <- factor(dc$sexn, levels = c(1,2), labels = c("Women", "Men"))

# make the plot
ggplot(data=dc, aes(y=cont, factor(cod14n), label=pctgapc)) + 
  geom_bar(stat="identity", colour="black", fill="grey80", width=0.5) + 
  geom_text(y=.35, hjust=1, size=2.5) + 
  annotate("text", x = 16, y = .35, label = "% of Total", hjust=1, size=2.5) +
  geom_segment(aes(x = 0, y = 0.005, xend = 0, yend = 0.025), size=0.25, arrow = arrow(length = unit(0.1, "cm"))) +
  annotate("text", x = 0, y = .03, label = "Decreased life expectancy", hjust=0, size=2) +
  geom_segment(aes(x = 0, y = -0.005, xend = 0, yend = -0.025), size=0.25, arrow = arrow(length = unit(0.1, "cm"))) +
  annotate("text", x = 0, y = -.03, label = "Increased", hjust=1, size=2) +
  xlab("Cause of death") + ylab("Years of life expectancy") + 
  facet_wrap( ~ class, ncol=2) + coord_flip() + scale_x_discrete(expand = c(0.1, 0.1)) + 
  scale_y_continuous(breaks=c(-.2,-.1,0,0.1,.2),limits=c(-.2,.35)) + theme_bw() +
  theme(axis.text.y=element_text(size=7), axis.ticks=element_blank(), strip.background = element_blank(), panel.grid.major.x = element_line(colour="grey80"), panel.grid.major.y=element_line(colour="white"), panel.grid.minor = element_line(colour="white"),panel.border = element_rect(colour = "black"))

# save plot
ggsave("cod-race.png", dpi=600, width=6.5, height=5.5)

